package com.sky.controller.user;

import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
@Api(tags = "菜品相关操作")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    public Result<List<DishVo>> getDishByCategoryId(@RequestParam(required = true) Long categoryId){
        String key="dish_"+categoryId;
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<DishVo> list = (List<DishVo>) valueOperations.get(key);
        if(list==null){
            list=dishService.queryDishByCategoryId(categoryId);
            valueOperations.set(key,list);
        }

        return Result.success(list);
    }
}
